package com.mantisframework.sys.action;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import jodd.util.StringUtil;

import org.apache.log4j.Logger;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.mantisframework.core.page.PageBean;
import com.mantisframework.core.struts.BaseAction;
import com.mantisframework.model.MSysUsers;
import com.mantisframework.sys.enumbean.UserSearchFields;
import com.mantisframework.sys.service.ISysUsersService;
import com.opensymphony.xwork2.ActionContext;

@Scope("prototype")
@Controller
public class SysUsersAction extends BaseAction {

	private static final long serialVersionUID = 1L;
	private static Logger _log = Logger.getLogger(SysUsersAction.class);
	private static final String LIST = "list";

	@Resource
	private ISysUsersService sysUsersService;
	private String op;
	private MSysUsers user;

	public SysUsersAction() {

	}

	public String add() {
		if ("add".equals(op)) {
			user = new MSysUsers();
			return "add2";
		} else if ("save".equals(op) || "update".equals(op)) {
			boolean flag = true;
			if("update".equals(op)) flag = false;
			boolean required = sysUsersService.validateRequired(user, flag);
			if (!required) {
				request.setAttribute("statusCode", 300);
				request.setAttribute("message",
						"请填写带*的必填项");
				return SUCCESS;
			}
			if (!"update".equals(op)) {
				boolean hasExists = sysUsersService.hasExistUser(user
						.getLoginName());
				if (hasExists) {
					request.setAttribute("statusCode", 300);
					request.setAttribute("message", "登陆名已存在");
					return SUCCESS;
				}
			}

			Integer ord = user.getUserOrd();
			if (null == ord)
				user.setUserOrd(10000);

			boolean isSuccess = false;
			if("save".equals(op)) {
				user.setCreateTime(new Date());
				isSuccess = sysUsersService.save(user);
			}
			else if("update".equals(op)) 
				isSuccess = sysUsersService.modifyUser(user);

			if(isSuccess) {
				return ajaxForwardSuccess("操作成功");
			} else {
				return ajaxForwardError("操作失败");
			}
		} else if ("modify".equals(op)) {
			user = sysUsersService.getPojoById(user.getId());
			return "add2";
		}
		return SUCCESS;
	}

	private Map<com.mantisframework.sys.enumbean.UserSearchFields, String> getCriterias() {
		Map<UserSearchFields, String> criterias = new HashMap<UserSearchFields, String>();
		if (user != null) {
			if (StringUtil.isNotEmpty(user.getUserName())) {
				criterias.put(UserSearchFields.userName, user.getUserName());
			}
			if (StringUtil.isNotEmpty(user.getLoginName())) {
				criterias.put(UserSearchFields.loginName, user.getLoginName());
			}
			if (user.getIsLock() != null) {
				criterias.put(UserSearchFields.isLock,
						String.valueOf(user.getIsLock()));
			}
		}

		return criterias;
	}

	public String list() {
		try {
			int pageNum = this.getPageNum() > 0 ? this.getPageNum() : 1;
			if (this.getNumPerPage() <= 0) {
				this.setNumPerPage(10);
			}
			Map<UserSearchFields, String> criterias = getCriterias();
//			PageBean pageBean = sysUsersService.querySysUsers(criterias,
//					new PageBean(pageNum, getNumPerPage()));
			PageBean pageBean = sysUsersService.querySysUsers(criterias,
					new PageBean(pageNum, getNumPerPage()),getOrderDirection(),getOrderField());
			ActionContext.getContext().put("userList", pageBean.getDatas());
			this.setTotalCount(pageBean.getTotalRecord());
			this.setTotalPage(pageBean.getTotalPage());
		} catch (Exception e) {
			_log.error(e.getMessage(), e);
		}
		return LIST;
	}

	public String view() {
		user = this.sysUsersService.getPojoById(user.getId());
		return SUCCESS;
	}

	public String delete() {
		try {
//			user = sysUsersService.getPojoById(user.getId());
			this.sysUsersService.deleteUserById(user.getId());
			return ajaxForwardSuccess("删除成功");
		} catch (Exception e) {
			_log.error(e.getMessage(), e);
			return ajaxForwardError("删除失败");
		}
//		return SUCCESS;
	}

	public void setSysUsersService(ISysUsersService sysUsersService) {
		this.sysUsersService = sysUsersService;
	}

	public String getOp() {
		return op;
	}

	public void setOp(String op) {
		this.op = op;
	}

	public MSysUsers getUser() {
		return user;
	}

	public void setUser(MSysUsers user) {
		this.user = user;
	}
}
